package com.css.apps.base.org.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.provider.OrgQueryProvider;
import com.css.apps.base.post.model.SPost;
import com.css.core.configuration.Environment;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class DirOrgPost extends AdminAction {
	private static Log log = LogFactory.getLog(DirOrgPost.class);
	private Page page;
	private String orgId;
	private String name;
	private List<SPost> PostList;

	public DirOrgPost() {
		page = new Page();
		page.setCurrentPage(1);
		page.setCountField("a.uuid");
	}

	@SuppressWarnings("unchecked")
	protected String adminGo() {
		if (StringHelper.isEmpty(orgId)) {
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
			return Action.ERROR;
		}
		try {
			List<String> orgPostList = OrgQueryProvider.queryPostId(orgId);// 岗位目前分配的岗位列表
			List<String> postIdList = new ArrayList<String>();
			if (StringHelper.isEmpty(name)) {
				for (String postid : orgPostList) {
					postIdList.add(postid.trim());
				}
			} else {
				for (String postid : orgPostList) {
					SPost post = QueryCache.get(SPost.class, postid.trim());
					if (post.getName().contains(name)) {
						postIdList.add(postid.trim());
					}
					if (name.equals(post.getName())) {
						postIdList.add(postid.trim());
					}
				}
			}
			if (postIdList.size() == 0) {
				PostList = null;
			} else {
				PostList = QueryCache.idToObj(SPost.class, postIdList);
			}
			return SUCCESS;
		} catch (Exception ex) {
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		}
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public List<SPost> getPostList() {
		return PostList;
	}

	public void setPostList(List<SPost> postList) {
		PostList = postList;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
